# TeamConnection stuff called from Custom Menu

# Modules required for the Dialogs
from etgui import *
from etguiimpl import PotGuiPopupButton
from PageLayout import *

import vcs, traceback, string
from vcs import NL
import TeamConnection
from  TeamConnection import getComponent
import TCSettings
from TCSettings import BuildCmdOptions, readSettings, writeSettings, TEST

gMod='TCCustom:'


# currently we don't use this function because of Sniff crash by customers
# (to large output for log window)
def showComponent(debug=TEST):
	try:
		marker = "showComponent"
		if debug > 0: 
			print "start", gMod+marker+NL
		
		print "Name	Owner	Description"
		print "----	-----	-----------"
		cmd = 'teamc report -view CompView -raw | awk  -F\'|\' \'{print $1"    "$2"    "$8}\''

		out = vcs.getoutput(cmd)
		# !!! this command crashes Sniff. The output is to large for log window.
		print out
		
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def showRelease(debug=TEST):
	try:
		marker = "showRelease"
		if debug > 0: 
			print "start", gMod+marker+NL
		
		print "Name	Owner	Description"
		print "----	-----	-----------"
		cmd = 'teamc report -view ReleaseView -raw | awk  -F\'|\' \'{print $1"     "$4"     "$10}\''
		out = vcs.getoutput(cmd)
		print out
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def freezeWorkArea(debug=TEST):
	try:
		marker = "freezeWorkArea"
		if debug > 0: 
			print "start", gMod+marker+NL
		settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		
		cmd = "teamc workarea -freeze %s -release  %s" % (area, rel)
		print "Freezing", rel
		rc = vcs.system(cmd)
		print "Exception in %s():" % marker
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def recreate(workingFile='', debug=TEST):
	try:
		marker = "recreate"
		if debug > 0: 
			print "start", gMod+marker+NL
		settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		
		cmd = "teamc part -recreate %s -release %s -workarea %s" % (workingFile, rel, area)
		print "Recreate", workingFile
		rc = vcs.system(cmd)
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def workingFileInfo(workingFile='', debug=TEST):
	try:
		marker = "workingFileInfo"
		if debug > 0: 
			print "start", gMod+marker+NL
		settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		
		cmd = "teamc part -view %s -long -release %s -workarea %s" % (workingFile, rel, area)
		out = vcs.getoutput(cmd)
		print out
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def commitedFileInfo(workingFile='', debug=TEST):
	try:
		marker = "commitedFileInfo"
		if debug > 0: 
			print "start", gMod+marker+NL
		settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		
		cmd = "teamc part -view %s -long -release %s " % (workingFile, rel)
		rc = vcs.system(cmd)
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def showFeature(debug=TEST):
	try:
		marker = "showFeature"
		if debug > 0: 
			print "start", gMod+marker+NL

		comp = ''
		retval, comp = getComponent(comp)
		if not retval:
			printLog('#### Function broken trough Component-Cancel ####')
			return FALSE 
		else:
			if debug > 1: print 'getComponent OK, comp: ', comp
		
		cmd = 'teamc report -view FeatureView -raw -where "state <> \'closed\' AND compName like \'%s\'" | awk  -F\'|\' \'{print $2" "$3"   "$6"	"$17"   "$4"     "$7}\''  % comp
		print
		print "Name	Component	State	Originator	Owner	Abstract"
		print "----	---------	-----	----------	-----	--------"
		out = vcs.getoutput(cmd)
		print out
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def showDefect(debug=TEST):
	try:
		marker = "showDefect"
		if debug > 0: 
			print "start", gMod+marker+NL
		
		comp = ''
		retval, comp = getComponent(comp)
		if not retval:
			printLog('#### Function broken trough Component-Cancel ####')
			return FALSE 
		else:
			if debug > 1: print 'getComponent OK, comp: ', comp
		
		cmd = 'teamc report -view DefectView -raw -where "compName like \'%s\'" | awk  -F\'|\' \'{print $2"	"$8"	"$6"	"$22"	"$5"	"$9}\''  % comp
		print "Name	Severity	State	Originator	Owner	Abstract"
		print "----	--------	-----	----------	-----	--------"
		out = vcs.getoutput(cmd)
		print out
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


def getUserName(user, debug=TEST):
	marker = "getUserName"
	if debug > 0: 
		print "start", gMod+marker+NL
	title = "TeamConnection"
	model = PotGuiModel()
	model.SetValue("User", user)
	# class textfield
	compField = PotTextField("User")
	retval=ShowSubDialog("%s" % title,
		model,
		MakePageLayout(eVObjHExpand | eVObjVExpand,
			MakeSectionLayout(
				Form(
					items=[
						"Enter user name:",compField,
					]	# items
				)	# Form
			),	# MakeSectionLayout
		)	# MakePageLayout
	)	# ShowSubDialog

	if retval:
		user = model.GetValue("User")
		ret = (retval, user)
	else:
		ret = (retval, '')
	
	if debug > 1: print "gMod\t" + marker + "@BretValue: @b%d@x@B" % retval
	if debug > 0: 
		print "end", gMod+marker+NL,
	return ret
	

def filesLockedByUser(debug=TEST):
	try:
		marker = "filesLockedByUser"
		if debug > 0: 
			print "start", gMod+marker+NL
		settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		
		userName = ''
		retval, userName = getUserName(userName)
		#retval = ret[0]
		if not retval:
			printLog('#### Function broken trough getUserName-Cancel ####')
			return FALSE 
		else:
			if debug > 1: print 'getUserName OK, userName: ', userName
		
		cmd = 'teamc report -view PartFullView -raw -where "userLogin like \'%s\' and releaseName like \'%s\'" | awk  -F\'|\' \'{print $8"   "$13}\''  % (userName, rel)
		if debug > 1: print 'cmd: ', cmd
		
		print
		print "Name			Locked by"
		print "----			---------"
		out = vcs.getoutput(cmd)
		print out
		if debug > 0: 
			print "end", gMod+marker+NL
	except:
		print "Exception in %s():" % marker
		traceback.print_exc()


if __name__=='__main__':
	import TCReload
	TCReload.ReloadAll()
	showComponent(2)
	pass



